$(function () {
    //提交按钮点击事件
    $("#submitBtn").click(function () {
        //获得文本框的值
        var brokername = $("#brokername").val();
        var decs = $("#decs").val();
        var brokerid = $("#brokerid").val();
        var seatid = $("#seatName").val();
        //获取当前选项名
        var seatname = $("#seatName option:selected").text();
        //判断是否存在Id,有Id调用修改的方法，没Id调用添加
        if (brokerid.length == 0) {//添加
            if (brokername == "" || brokername == null) {
                layer.msg("请填写商券名");
            } else {
                $.ajax({
                    url: "broker/add",
                    type: "post",
                    data: {"brokername": brokername, "decs": decs, "seatid": seatid, "seatname": seatname},
                    success: function (data) {
                        layer.msg(data, {time: 2000}, function () {
                            window.location.reload();
                        });

                    }
                })
            }
        } else {//修改
            $.ajax({
                url: "broker/updata",
                type: "post",
                data: {"brokername": brokername, "decs": decs, "brokerid": brokerid},
                success: function (data) {
                    layer.msg(data);
                    //刷新页面
                    window.location.reload();
                }
            })
        }
    });


    /*-------------表字段数据开始----------------*/
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: '/broker/selectAll'//控制器中条件查询的分页列表
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '券商表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {type: 'numbers', title: '序号'}
                , {field: 'brokerid', title: 'ID', unresize: true}
                , {field: 'seatid', title: 'id', edit: 'text'}
                , {field: 'seatname', title: '交易席位名'}
                , {field: 'brokername', title: '券商名'}
                , {field: 'decs', title: '描述'}

            ]]
            //开启分页
            , page: true
            , id: "dataTable"
        });
        /*------------------表字段结束---------------*/

        /*--------------------模糊查询开始----------------------*/
        //模糊查询
        $("#seekBtn").click(function () {
            //获得输入框的值
            var d = $("#demoReload").val()
            table.reload("dataTable", {
                url: "/broker/selectLike"
                , where: {
                    condition: d
                }
            });
        });
        /*----------------------模糊查询结束----------------------------*/

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var layer = layui.layer;
            var data = checkStatus.data;
            $.ajax({
                url: "broker/getName",
                type: 'post',
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var datum = data[i];
                        $("#seatName").append("<option value=" + datum.seatid + ">" + datum.seatname + "</option>")
                    }
                    layui.form.render('select');//注意渲染页面表单，否则不显示数据
                }
            })
            switch (obj.event) {
                //编辑框
                case 'getCheckData':

                    if (data.length > 1) {
                        layer.msg("一次只能修改一个");
                        break;
                    } else if (data.length < 1) {
                        layer.msg("请选择一条需要修改的数据");
                        break;
                    } else {
                        layer.open({
                            type: 1,
                            title: '修改管理人',
                            content: $("#saveOrUpdateDiv"),
                            area: ['800px', '400px'],
                        })
                        $("#seatName").children().remove();
                        $("#brokerid").val(data[0].brokerid);
                        $("#brokername").val(data[0].brokername);
                        $("#decs").val(data[0].decs);
                        break;
                    }
                //添加页面
                case 'add':
                    var data = checkStatus.data;
                    layer.open({
                        type: 1,
                        title: '添加管理人',
                        content: $("#saveOrUpdateDiv"),
                        area: ['800px', '400px'],
                    });
                    //清空文本框的值
                    $(".layui-input-inline input,textarea").val("")
                    $("#seatName").children().remove();
                    break;
                //删除
                case 'delete':
                    //获得复选框所选数组
                    var check = table.checkStatus("dataTable"), data = checkStatus.data;
                    if (data == "") {
                        layer.msg("至少选择一行进行操作！！！", {icon: 2});
                        return;
                        break;
                    }
                    //被选中的
                    var ids = "";
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].brokerid + ",";
                            console.log(ids);
                        }
                    }
                    layer.confirm("确定要删除当前选中的数据吗?" + ids, function (index) {
                        $.ajax({
                            type: 'post',
                            url: 'broker/delete',
                            data: {"ids": ids.substr(0, ids.length - 1)},
                            success: function (data) {
                                layer.msg("删除成功", {icon: 1});
                                // $(".layui-form-checked").not(".header").parents("tr").remove();
                                window.location.reload();
                            },
                            error: function (data) {
                                alert("失败");
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });
        /*-------------------头工具栏事件结束---------------------*/

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });


});
